# coding: utf-8

from utils import ztjy_db_connection_pool
import config.setting as setting
import logging


def nlp_write(db_table, columns, write_params):
    '''
    nlp结果入库接口
    :param write_params: 参考insert_columns
    :return: None
    '''
    conn = ztjy_db_connection_pool.get_connection(setting.DB_HOST, setting.DB_PORT, setting.DB_USER,
                                                  setting.DB_PASSWORD)
    replace_sql = 'replace into {db_table} ({columns}) values ({params_format})'.format(
        db_table=db_table,
        columns=','.join(columns),
        params_format=get_column_format(columns)
    )
    cur = conn.cursor()

    index = 0
    while index < len(write_params):
        l = write_params[index:index + 100]
        index += len(l)
        cur.executemany(replace_sql, l)
        conn.commit()

    cur.close()
    conn.close()


def get_column_format(columns):
    params_format = ['%s'] * len(columns)
    return ','.join(params_format)


def record_location(content_type, origin_id, update_time):
    logging.info('write: {} {} {}'.format(content_type, origin_id, update_time))
    conn = ztjy_db_connection_pool.get_connection(setting.DB_HOST, setting.DB_PORT, setting.DB_USER,
                                                  setting.DB_PASSWORD)
    cur = conn.cursor()
    columns = ['content_type', 'location_id', 'location_update_time']
    sql = 'replace into dbnlp.t_deal_location ({columns}) values ({params_format})'.format(
        columns=','.join(columns),
        params_format=get_column_format(columns)
    )
    logging.info(sql)
    logging.info((content_type, origin_id, update_time))
    cur.executemany(sql, [(content_type, origin_id, update_time)])
    conn.commit()

    cur.close()
    conn.close()


def get_last_location(content_type):
    conn = ztjy_db_connection_pool.get_connection(setting.DB_HOST, setting.DB_PORT, setting.DB_USER,
                                                  setting.DB_PASSWORD)
    cur = conn.cursor()
    columns = ['content_type', 'location_id', 'location_update_time']
    sql = 'select {columns} from dbnlp.t_deal_location where content_type = {content_type}'.format(
        columns=','.join(columns),
        content_type=content_type
    )
    cur.execute(sql)
    res = cur.fetchone()
    cur.close()
    conn.close()
    if res is None:
        return content_type, 0, '1970-01-01 08:00:00'
    else:
        return res


def nlp_read(content_type, db_table, columns, limit):
    conn = ztjy_db_connection_pool.get_connection(setting.DB_HOST, setting.DB_PORT, setting.DB_USER,
                                                  setting.DB_PASSWORD)
    cur = conn.cursor()

    content_type, location_id, location_update_time = get_last_location(content_type=content_type)
    sql = "select {columns} from {db_table} where update_time > '{location_update_time}' order by update_time asc limit {limit}".format(
        columns=','.join(columns),
        db_table=db_table,
        location_update_time=location_update_time,
        limit=limit,
    )
    logging.info('{} {} {}'.format(content_type, location_id, location_update_time))
    cur.execute(sql)
    all_data = cur.fetchall()
    cur.close()
    conn.close()
    return columns, all_data


def nlp_offline_read(content_type, db_table, columns, limit):
    conn = ztjy_db_connection_pool.get_connection(setting.DB_HOST, setting.DB_PORT, setting.DB_USER,
                                                  setting.DB_PASSWORD)
    cur = conn.cursor()

    sql = "select {columns} from {db_table} where is_delete = 0 and is_online = 1 order by update_time asc limit {limit}".format(
        columns=','.join(columns),
        db_table=db_table,
        limit=limit,
    )
    cur.execute(sql)
    all_data = cur.fetchall()
    cur.close()
    conn.close()
    return columns, all_data
